package com.ws.wsmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 过滤器：
 * 配置允许跨域
 */
@Configuration  //该类为配置类。
public class WsmallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource urlSource = new UrlBasedCorsConfigurationSource();
        //在这个里面配置所有的跨域规则
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        //请求来源
        corsConfiguration.addAllowedOrigin("*");
        //允许携带cookie完成跨域
        corsConfiguration.setAllowCredentials(true);
        urlSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(urlSource);
    }

}
